﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace System
{
    public class ZRegex
    {
        public static bool IsMatch(string input, string sRegex)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mc = re.Match(input);
            return mc.Success;
        }

        public static List<string> GetList(string input, string sRegex, int iGroupIndex)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mcs = re.Matches(input);
            return (from Match mc in mcs select iGroupIndex > 0 ? mc.Groups[iGroupIndex].Value : mc.Value).ToList();
        }

        public static List<string> GetList(string input, string sRegex, string sGroupName)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mcs = re.Matches(input);
            return
                (from Match mc in mcs select string.IsNullOrEmpty(sGroupName) ? mc.Value : mc.Groups[sGroupName].Value).
                    ToList();
        }

        public static string GetText(string input, string sRegex, int iGroupIndex)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mc = re.Match(input);
            return mc.Success ? (iGroupIndex > 0 ? mc.Groups[iGroupIndex].Value : mc.Value) : "";
        }

        public static string GetText(string input, string sRegex, string sGroupName)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mc = re.Match(input);
            return mc.Success ? (string.IsNullOrEmpty(sGroupName) ? mc.Value : mc.Groups[sGroupName].Value) : "";
        }

        public static string Replace(string input, string sRegex, string sReplace, int iGroupIndex)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mcs = re.Matches(input);
            return mcs.Cast<Match>().Aggregate(input,
                                               (current, mc) =>
                                               current.Replace(
                                                   iGroupIndex > 0 ? mc.Groups[iGroupIndex].Value : mc.Value, sReplace));
        }

        public static string Replace(string input, string sRegex, string sReplace, string sGroupName)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var mcs = re.Matches(input);
            return mcs.Cast<Match>().Aggregate(input,
                                               (current, mc) =>
                                               current.Replace(
                                                   string.IsNullOrEmpty(sGroupName)
                                                       ? mc.Value
                                                       : mc.Groups[sGroupName].Value, sReplace));
        }

        public static List<string> Split(string input, string sRegex, int iStrLen)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var sArray = re.Split(input);
            var list = new List<string>();
            list.Clear();
            list.AddRange(from s in sArray where s.Trim().Length >= iStrLen select s.Trim());
            return list;
        }

        public static List<string> Split(string input, string sRegex)
        {
            var re = new Regex(sRegex,
                               RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            var sArray = re.Split(input);
            var list = new List<string>();
            list.Clear();
            list.AddRange(sArray.Select(s => s.Trim()));
            return list;
        }

        public static string ClearTag(string input)
        {
            return Replace(input, @"</?[a-z]+[^>]*>", "", 0);
        }
    }
}
